package com.dyh.quickdemozwei.webdav;

import java.io.File;

import org.apache.http.HttpEntity;

import org.apache.http.HttpHost;

import org.apache.http.HttpResponse;

import org.apache.http.auth.AuthScope;

import org.apache.http.auth.NTCredentials;

import org.apache.http.client.CredentialsProvider;

import org.apache.http.client.methods.HttpDelete;

import org.apache.http.client.methods.HttpHead;

import org.apache.http.client.methods.HttpPut;

import org.apache.http.entity.FileEntity;

import org.apache.http.impl.client.BasicCredentialsProvider;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.protocol.BasicHttpContext;

import org.apache.http.protocol.HttpContext;

import org.springframework.util.StringUtils;

public class WebDavNTLMUtils {

    private static String WEBDAV_ROOTPATH = "http://ip:8001";

    private static String WEBDAV_USER = "用户名";

    private static String WEBDAV_PWD = "密码";

    private static String host;

    private static Integer prot;

    private static String prefix;

    private static WebDavNTLMUtils webDavUtil;

    private void init() {

        host = WEBDAV_ROOTPATH.substring(WEBDAV_ROOTPATH.indexOf("//") + 2, WEBDAV_ROOTPATH.lastIndexOf(":"));

        prot = Integer.valueOf(WEBDAV_ROOTPATH.substring(WEBDAV_ROOTPATH.lastIndexOf(":") + 1, WEBDAV_ROOTPATH.length()));

        prefix = WEBDAV_ROOTPATH.substring(0, WEBDAV_ROOTPATH.indexOf(":"));

    }

    private WebDavNTLMUtils() {

        init();

    }

    public static WebDavNTLMUtils initialization(String rootPath, String user, String pwd) {

        if (webDavUtil == null) {

            if (StringUtils.isEmpty(rootPath) || StringUtils.isEmpty(user) || StringUtils.isEmpty(pwd)) {

                webDavUtil = new WebDavNTLMUtils();

            } else {

                webDavUtil = new WebDavNTLMUtils(rootPath, user, pwd);

            }

        }

        return webDavUtil;

    }

    private WebDavNTLMUtils(String rootPath, String user, String pwd) {

        WEBDAV_ROOTPATH = rootPath;

        WEBDAV_USER = user;

        WEBDAV_PWD = pwd;

        init();

    }

    /**
     * WEB DAV 上传文件
     *
     * @param sourcepath 文件地址绝对路径
     * @param destpath   服务器文件地址加文件名
     * @throws Exception
     */

    public void upload(String sourcepath, String destpath) throws Exception {

        String path = WEBDAV_ROOTPATH + destpath;

        CredentialsProvider credsProvider = new BasicCredentialsProvider();

        NTCredentials creds = new NTCredentials(WEBDAV_USER, WEBDAV_PWD, "", "");

        credsProvider.setCredentials(new AuthScope(host, prot, null, "ntlm"), creds);

        CloseableHttpClient httpclient = HttpClients.custom()

                .setDefaultCredentialsProvider(credsProvider)

                .build();

        HttpHost target = new HttpHost(host, prot, prefix);

// 保证相同的内容来用于执行逻辑相关的请求

        HttpContext localContext = new BasicHttpContext();

        HttpPut httpPut = new HttpPut(path);

        File file = new File(sourcepath);

        HttpEntity entity = new FileEntity(file);

        httpPut.setEntity(entity);

        httpclient.execute(target, httpPut, localContext);

        httpclient.close();

    }

    /**
     * 删除服务器文件
     *
     * @param destpath 服务器文件地址
     * @throws Exception
     */

    public void delete(String destpath) throws Exception {

        String path = WEBDAV_ROOTPATH + destpath;

        CredentialsProvider credsProvider = new BasicCredentialsProvider();

        NTCredentials creds = new NTCredentials(WEBDAV_USER, WEBDAV_PWD, "", "");

        credsProvider.setCredentials(new AuthScope(host, prot, null, "ntlm"), creds);

        CloseableHttpClient httpclient = HttpClients.custom()

                .setDefaultCredentialsProvider(credsProvider)

                .build();

        HttpHost target = new HttpHost(host, prot, prefix);

// 保证相同的内容来用于执行逻辑相关的请求

        HttpContext localContext = new BasicHttpContext();

        HttpDelete httpDelete = new HttpDelete(path);

        httpclient.execute(target, httpDelete, localContext);

        httpclient.close();

    }

    /**
     * 创建文件夹
     *
     * @param destpath 服务器文件夹路径
     * @throws Exception
     */

//    public void createFolder(String destpath) throws Exception {
//
//        String path = WEBDAV_ROOTPATH + destpath;
//
//        CredentialsProvider credsProvider = new BasicCredentialsProvider();
//
//        NTCredentials creds = new NTCredentials(WEBDAV_USER, WEBDAV_PWD, "", "");
//
//        credsProvider.setCredentials(new AuthScope(host, prot, null, "ntlm"), creds);
//
//        CloseableHttpClient httpclient = HttpClients.custom()
//
//                .setDefaultCredentialsProvider(credsProvider)
//
//                .build();
//
//        HttpHost target = new HttpHost(host, prot, prefix);
//
//// 保证相同的内容来用于执行逻辑相关的请求
//
//        HttpContext localContext = new BasicHttpContext();
//
//        HttpMkcol httpMkcol = new HttpMkcol(path);
//
//        httpclient.execute(target, httpMkcol, localContext);
//
//        httpclient.close();
//
//    }

    /**
     * 判断文件夹或者文件是否存在
     *
     * @param destdir 服务器地址
     * @return 200存在，其它不存在
     */

    public int checkDirExist(String destdir) {

        try {

            String path = WEBDAV_ROOTPATH + destdir;

            CredentialsProvider credsProvider = new BasicCredentialsProvider();

            NTCredentials creds = new NTCredentials(WEBDAV_USER, WEBDAV_PWD, "", "");

            credsProvider.setCredentials(new AuthScope(host, prot, null, "ntlm"), creds);

            CloseableHttpClient httpclient = HttpClients.custom()

                    .setDefaultCredentialsProvider(credsProvider)

                    .build();

            HttpHost target = new HttpHost(host, prot, prefix);

// 保证相同的内容来用于执行逻辑相关的请求

            HttpContext localContext = new BasicHttpContext();

            HttpHead httpHead = new HttpHead(path);

            HttpResponse response = httpclient.execute(target, httpHead, localContext);

            httpclient.close();

            return response.getStatusLine().getStatusCode();

        } catch (Exception e) {

        }

        return 0;

    }

    /**
     * 远程服务器copy
     *
     * @param source   源文件
     * @param destpath 目标文件
     * @throws Exception
     */
//
//    public void copy(String source, String destpath) throws Exception {
//
//        String path = WEBDAV_ROOTPATH + source;
//
//        CredentialsProvider credsProvider = new BasicCredentialsProvider();
//
//        NTCredentials creds = new NTCredentials(WEBDAV_USER, WEBDAV_PWD, "", "");
//
//        credsProvider.setCredentials(new AuthScope(host, prot, null, "ntlm"), creds);
//
//        CloseableHttpClient httpclient = HttpClients.custom()
//
//                .setDefaultCredentialsProvider(credsProvider)
//
//                .build();
//
//        HttpHost target = new HttpHost(host, prot, prefix);
//
//// 保证相同的内容来用于执行逻辑相关的请求
//
//        HttpContext localContext = new BasicHttpContext();
//
//        HttpCopy httpCopy = new HttpCopy(path);
//
//        httpCopy.addHeader("Destination", destpath);
//
//        httpclient.execute(target, httpCopy, localContext);
//
//        httpclient.close();
//
//    }

//    /**
//     * 远程服务器move
//     *
//     * @param source   源文件
//     * @param destpath 目标文件
//     * @throws Exception
//     */
//
//    public void move(String source, String destpath) throws Exception {
//
//        String path = WEBDAV_ROOTPATH + source;
//
//        CredentialsProvider credsProvider = new BasicCredentialsProvider();
//
//        NTCredentials creds = new NTCredentials(WEBDAV_USER, WEBDAV_PWD, "", "");
//
//        credsProvider.setCredentials(new AuthScope(host, prot, null, "ntlm"), creds);
//
//        CloseableHttpClient httpclient = HttpClients.custom()
//
//                .setDefaultCredentialsProvider(credsProvider)
//
//                .build();
//
//        HttpHost target = new HttpHost(host, prot, prefix);
//
//// 保证相同的内容来用于执行逻辑相关的请求
//
//        HttpContext localContext = new BasicHttpContext();
//
//        HttpMove httpMove = new HttpMove(path);
//
//        httpMove.addHeader("Destination", destpath);
//
//        httpclient.execute(target, httpMove, localContext);
//
//        httpclient.close();
//
//    }

    public static void main(String[] args) throws Exception {

        WebDavNTLMUtils web = new WebDavNTLMUtils();

        System.out.println();

       // web.move("/UploadFile/User/ccfff.jpg", "/UploadFile/News/20191202/cc.jpg");

//int code=web.checkDirExist("/UploadFile/Kind/2019");

//web.createFolder("/UploadFile/Kind/2019");

//System.out.println(code);

//web.delete("/UploadFile/Kind/test8.png");

//web.upload("C:\\Users\\g\\Desktop\\新建文件夹\\9959fa7a-0d17-4a61-86bf-7abf92cd9ce2.JPG", "/UploadFile/Kind/2019/aafff1122.png");

    }

}
